define(function(require,exports,module) {
    var myPub = require('./public');
    var scrollLoad = require('./scrollLoading');
    var billDateUrl = '/prepaidCard/consumerDetails';  // 模拟数据
    var oWindow = $(window);                        // windows
    var lazLoading = new myPub.lazLoadingImg();
    var oHeader = $('.js_headerBox');               // 顶部
    var oContain = $('.js_contain');                // 内容部分
    var obillDetail = $('.js_billDetail');          // 可以滚动模块
    var oScrollBox = $('.js_scrollBox');            // 需要滚动时最外层添加
    var owrap = $('.js_wrap');                      // 有数据时显示
    var oLoadingNull = $('.js_loadingBtn').eq(0);   // 没有更多选项
    var oLoading = $('.js_loadingBtn').eq(1);       // 加载按钮
    var oErrPage = $('.js_errPage');                // 无数据的情况

    // 页面内容距顶部的尺寸
    var resizeTimer = null;
    oWindow.on('resize',function(){
        if (resizeTimer) {
            clearTimeout(resizeTimer)
        }
        resizeTimer = setTimeout(function(){
            oContain.css({
                'padding-top': oHeader.outerHeight(true)
            });
        }, 400);
    });

    lazLoading.AddLazLoadingImg(oScrollBox[0]);


    module.exports = {
        init:function(){
            connectHtml.AjaxData(connectHtml.start, connectHtml.limit);
            scrollLoad.oScrollLoading.ScrollPage(function () {
                if(!connectHtml.bLoadOff) {
                    connectHtml.dataJson = null;
                    return;
                }
                // 滚动底部页数增加 起始数据等于上次结束数据  新的结束等于页数*页码数
                connectHtml.start = ++connectHtml.start;//connectHtml.limit*connectHtml.pagearr;
                connectHtml.pagearr++;
                connectHtml.AjaxData(connectHtml.start, connectHtml.limit);
            });
        }
    };
    var connectHtml = {
        limit: 10,                       // 每次加载多少数据
        getData: [],                    // 数据记录
        pagearr: 1,                     // 当前页码
        pageCout: 0,                    // 页码总数
        start:1,                        // 开始数据
        end: 0,                         // 结束数据
        time_Scroll:null,               // 计时器
        bLoadOff: true,                 // 加载开关
        // 设置广告数据
        AjaxData: function (start,limit,fn) {
            var _this = this;
            G_JSON_POST(billDateUrl,{
                "start":_this.start,
                "limit":_this.limit
            },function (data) {

                if( data.status == "error") {
                    owrap.hide();
                    oErrPage.show();
                    return false;
                }
                //存储数据
                if( data.status == "success") {
                    _this.getData = data.obj;
                   // console.log(_this.getData);
                    // 对应数据为0 return
                    if(_this.getData.length == 0 && _this.pagearr < 2) {
                        _this.bLoadOff = false;
                        owrap.hide();
                        oErrPage.show();
                        return
                    }
                    if( _this.pagearr >= 1) {
                        if(_this.getData.length == 0){
                            oLoadingNull.show();
                            oLoading.hide();
                            _this.bLoadOff = false;
                            return
                        }
                    }
                    if(_this.getData.length <  _this.limit) {
                        oLoading.hide();
                        oLoadingNull.show();
                        _this.bLoadOff = false;
                    }
                    //拼接页面
                    _this.GoodsLiBoxHtml(_this.getData);
                    // oLoading.show();
                    data = null;
                    if(fn)fn();
                }
            }, function () {
                // start
            }, function () {
                // end
            });
        },
        // 正常的数据
        GoodsLiBoxHtml : function (data, fn) {
            var _this = this;
            var shtml = '';
            for(var i=0; i<data.length; i++) {
                var date = myPub.FormatDateTime(new Date(data[i].createTime),'minSec');
                shtml +='<div class="bill flexBox spBe BoxCenter"><div class="billImg"> <img src="'+checkImage(data[i].recordType)+'" alt=""></div>'+
                    '       <div class="detailContain flexBox spBe BoxCenter"><div class="detailTxt"><p>'+checkType(data[i].recordType,data[i].severType)+'</p><p> <i>'+date+'</i>'+
                    '                </p></div>'+checkName(data[i].recordType,data[i].money.toFixed(2))+'</div></div>'

            }

            obillDetail.append(shtml);
            lazLoading.DestroyLazLoadingImg();
            lazLoading.AddLazLoadingImg(oScrollBox[0]);

            var nclientHeight = oScrollBox.height();
            var ncontainHeight = oContain.outerHeight(true);
            if( ncontainHeight < nclientHeight ) {
                _this.start = _this.pagearr * _this.limit;
                _this.pagearr++;
                _this.AjaxData(_this.start,_this.limit);
            }
            if(fn)fn();
        }
    };
    function checkOrderSn(orderSn) {
       return orderSn!=""?'['+orderSn+']':'';
    }

    function checkImage( operation) {
        if(operation =="1"){
            return "../static_v4/image/cashCards/shopping.png"
        }else if (operation =="2"){
            return "../static_v4/image/cashCards/refund.png"
        }else if (operation =="3"){
            return "../static_v4/image/cashCards/recharge.png"
        }
    }

    function checkType(operation,type) {
        var soure = severType(type)
        if(operation =="1"){
            return soure+"购物"
        }else if (operation =="2"){
            return soure+"退款"
        }else if (operation =="3"){
            return soure+"充值"
        }
    }
    
    function severType(type) {
        if(type =="1"){
            return "大当家"
        }else if (type =="2"){
            return "小当家"
        }
    }

    function checkName(operation,money) {
        if(operation =="1"){
            return '<div style="color: red;" class=\"detailNo\"><p>-'+money+'</p></div>';
        }else if (operation =="2"){
            return '<div  class=\"detailNo\"><p>+'+money+'</p></div>';
        }else if (operation =="3"){
            return '<div style="color: red;" class=\"detailNo\"><p>+'+money+'</p></div>';
        }


    }


});


